define(['jquery', 'bootstrap', 'backend', 'table', 'form', 'bootstrap-table-jumpto'], function ($, undefined, Backend, Table, Form, Jump) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'shop/order/index' + location.search,
                    add_url: 'shop/order/add',
                    edit_url: 'shop/order/edit',
                    del_url: 'shop/order/del',
                    multi_url: 'shop/order/multi',
                    table: 'shop_order',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                showExport: false,
                showJumpto: true,
                searchFormVisible: true,
                detailView: true,//父子表
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'order_no', title: __('Order_no')},
                        /*{field: 'user_id', title: __('User_id')},*/
                        {field: 'user.nickname', title: '用户昵称'},
                        {field: 'amount', title: __('Amount'), operate:'BETWEEN'},
                        /*{field: 'address_id', title: __('Address_id')},*/
                        {field: 'status', title: __('Status'), searchList: {"0":__('Status 0'),"1":__('Status 1'),"2":__('Status 2'),"3":__('Status 3')}, formatter: Table.api.formatter.status},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},

                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ],
                //注册加载子表的事件。
                onExpandRow: function(index, row, $detail) {
                    var html = "";
                    var tScore = (row.buy_num != null) ? row.buy_num : '-';
                    var tMoney = (row.amount != null) ? row.amount : '-';
                    var name = (row.address != null) ? (row.address.name != null ? row.address.name : '-'):'-';
                    var mobile = (row.address != null) ? (row.address.mobile != null ? row.address.mobile : '-'):'-';
                    var address = (row.address != null) ? (row.address.address_text != null ? row.address.address_text : '-') : '-';
                    html += "<div class=\"col-xs-8\"><ul style='list-style:none; padding:0px; margin:0px;'>";
                    $.ajax({
                        type: "post",
                        url: "shop/order/getOrderGoods", //子表请求的地址
                        data: {id:row.id}, //我这里是点击父表后，传递父表列id给后台查询子表数据
                        dataType: 'json',
                        //async: false, //同步请求
                        success: function(data) {
                            //遍历子表数据

                            $.each(data, function(n, value) {
                                var goodsURL = "shop/goods/details?gid=" + value.goods_id;
                                html += '<li style="height:70px; padding:5px 0;"><div class="col-xs-2"><a  href="javascript:;"><img src="'+ value.goods_thumb +'" style="height:60px; width:80px;"/>'
                                    +'</a></div>'
                                    +'<div class="col-xs-4"><a  href="javascript:;">'
                                    +value.goods_name
                                    +'</a></div>'
                                    + '<div class="col-xs-2">'
                                    + '价格' + ": "
                                    +value.money
                                    +'</div>'

                                    + '<div class="col-xs-2">*'
                                    +value.buy_num
                                    +'</div></li>';
                            });
                            html += '</ul></div>';
                            html += '<div class="col-xs-4">'

                                +'<div class="col-xs-12">'
                                + '实际支付金额'
                                +': '
                                + tMoney
                                +'</div><br><br><br>'
                                +'<div class="col-xs-12">'
                                +'收件人'
                                +': '
                                + name
                                +'</div>'
                                +'<div class="col-xs-12">'
                                + '手机'
                                +': '
                                + mobile
                                +'</div><br/><br/><br/>'
                                +'<div class="col-xs-12">'
                                + address
                                +'</div>'
                                +'</div>';
                            $detail.html(html); // 关键地方
                        }
                    });
                },

            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },




        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});